package basic50.thirtieth;

import java.util.Random;

public class Basic29 {
    public static void main(String[] args) {
        //题目：求一个3*3矩阵对角线元素之和
       // 程序分析：利用双重for循环控制输入二维数组，再将a[i][i]累加后输出。
        int n=3;
        int[][] arr= new int[n][n];
        Random rand = new Random();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int x = rand.nextInt(10);
                arr[i][j] = x;
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }

        int sum =0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if( ((i ==0 || i == n-1) && ( j ==0 || j==n-1 ) || i==j) ){
                    sum+=arr[i][j];
                }
            }
        }
        System.out.println(sum);
    }
}
